const express=require('express')
const db=require('../db/index')
const router=express.Router()

router.get('/getAllArticle',(req,res)=>{
        db.query('select article_tab.id,title,about,article,tags,issueData,authorId,username,user_pic from article_tab,users where users.id=article_tab.authorId',(err,result)=>{
            if(err){
                return req.resSend(401,err)
            }
            if(result===0){
                return req.resSend(404,'暂无相应数据')
            }
           res.send({
            status:200,
            msg:'数据获取成功',
            data:result
           })
        })
})

router.get('/getClassify',(req,res)=>{
    db.query('select about from article_tab',(err,result)=>{
        if(err){
            return req.resSend(401,err)
        }
        if(result.length===0){
            return req.resSend(404,'暂无数据！')
        }
        res.send({
            status:200,
            msg:'文章类型查询成功！',
            data:result
        })
    })
})

router.get('/getclassifyArticles',(req,res)=>{
    console.log(req.query.about)
    db.query('select article_tab.id,title,about,article,tags,issueData,authorId,username,user_pic from article_tab,users where users.id=article_tab.authorId and about=?',req.query.about,(err,result)=>{
        if(err){
            return req.resSend(401,err)
        }
        if(result.length===0){
            return req.resSend(404,'暂无该分类文章！')
        }
        res.send({
            status:200,
            msg:'查询成功！',
            data:result
        })
    })
})

router.get('/getOneArticle',(req,res)=>{
    db.query('select article_tab.id,title,about,article,tags,issueData,authorId,username,user_pic from article_tab,users where users.id=article_tab.authorId and article_tab.id=?',req.query.id*1,(err,result)=>{
        if(err){
            return req.resSend(401,err)
        }
        if(result.length===0){
            return req.resSend(404,'文章查询失败！')
        }
        res.send({
            status:200,
            msg:'文章查询成功！',
            data:result[0]
        })
    })
})

module.exports=router